# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=antlr3
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.5.3
pkgrel=1
pkgdesc="ANother Tool for Language Recognition (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.antlr3.org/"
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/antlr/${_realname}/archive/${pkgver}.tar.gz")
sha256sums=('a0892bcf164573d539b930e57a87ea45333141863a0dd3a49e5d8c919c8a58ab')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  cd "${srcdir}"
  tar xf "${_realname}-${pkgver}.tar.gz" "${_realname}-${pkgver}/runtime/C"

  cd "${_realname}-${pkgver}/runtime/C"
  sed -e "s/AM_INIT_AUTOMAKE(foreign)/AM_INIT_AUTOMAKE([foreign subdir-objects])/g" \
      -i configure.ac
  sed -e "s/\(libantlr3c_la_LDFLAGS.*\)/\1 -no-undefined\nlibantlr3c_la_LIBADD = -lws2_32/g" \
      -e "s/INCLUDES = -Iinclude/AM_CPPFLAGS = -I\$(srcdir)\/include/g" \
      -i Makefile.am

  autoreconf -fvi
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  ../${_realname}-${pkgver}/runtime/C/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-abiflags

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/runtime/C/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
